package com.bsb.acc.inner.impl;

import com.bsb.acc.dao.AccTopPageConfigDao;
import com.bsb.acc.domain.AccTopPageConfig;
import com.bsb.acc.domain.AccTopPageConfigCriteria;
import com.bsb.acc.dto.AccTopPageShowDto;
import com.bsb.acc.errorcode.BusinessErrorCode;
import com.bsb.acc.service.AccTopPageConfigService;
import com.smy.framework.core.exception.ServiceException;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 置顶位背景配置业务层实现类
 *
 * @Author: ligc
 * @Date 2019/2/13
 * @Description
 */
@Service("accTopPageConfigService")
public class AccTopPageConfigServiceImpl implements AccTopPageConfigService {

    @Resource
    private AccTopPageConfigDao accTopPageConfigDao;

    @Override
    public AccTopPageConfig save(String returnDir, String user, Integer type) {
        AccTopPageConfigCriteria accTopPageConfigCriteria = new AccTopPageConfigCriteria();
        AccTopPageConfigCriteria.Criteria criteria = accTopPageConfigCriteria.createCriteria();
        criteria.andStatusEqualTo(2);
        criteria.andDeleteEqualTo(0);
        criteria.andTypeEqualTo(type);
        List<AccTopPageConfig> list = accTopPageConfigDao.selectByCriteria(accTopPageConfigCriteria);
        if (null != list && !list.isEmpty()) {
            throw new ServiceException(BusinessErrorCode.FAILURE.getCode(), "已存在该类型置顶位背景配置");
        }
        AccTopPageConfig accTopPageConfig = new AccTopPageConfig();
        accTopPageConfig.setImageUrl(returnDir);
        accTopPageConfig.setCreatedTime(new Date());
        accTopPageConfig.setType(type);
        accTopPageConfig.setCreateUser("user");
        accTopPageConfig.setUpdatedTime(new Date());
        accTopPageConfig.setStatus(2);
        int insert = accTopPageConfigDao.insert(accTopPageConfig);
        return accTopPageConfig;
    }

    @Override
    @Transactional
    public int publishAccTopPage(String user) {
        AccTopPageConfigCriteria accTopPageConfigCriteria = new AccTopPageConfigCriteria();
        AccTopPageConfigCriteria.Criteria criteria = accTopPageConfigCriteria.createCriteria();
        criteria.andStatusEqualTo(1);
        criteria.andDeleteEqualTo(0);
        List<AccTopPageConfig> publishedList = accTopPageConfigDao.selectByCriteria(accTopPageConfigCriteria);
        AccTopPageConfigCriteria accTopPageConfigCriteria1 = new AccTopPageConfigCriteria();
        AccTopPageConfigCriteria.Criteria criteria1 = accTopPageConfigCriteria1.createCriteria();
        criteria1.andDeleteEqualTo(0);
        criteria1.andStatusEqualTo(2);
        List<AccTopPageConfig> publishingList = accTopPageConfigDao.selectByCriteria(accTopPageConfigCriteria1);
        if (null != publishingList && !publishingList.isEmpty()) {
            Date date = new Date();
            if (null != publishedList && !publishedList.isEmpty()) {
                for (AccTopPageConfig entity : publishedList) {
                    entity.setStatus(2);
                    entity.setDelete(1);
                    entity.setUpdatedTime(date);
                    accTopPageConfigDao.updateByPrimaryKeySelective(entity);
                }
            }
            for (AccTopPageConfig entity : publishingList) {
                entity.setStatus(1);
                entity.setDelete(0);
                entity.setUpdatedTime(date);
                entity.setCreateUser(user);
                accTopPageConfigDao.updateByPrimaryKeySelective(entity);
            }
        } else {

        }
        return 0;
    }

    @Override
    @Transactional
    public int cancelAccTopPage() {
        AccTopPageConfigCriteria accTopPageConfigCriteria = new AccTopPageConfigCriteria();
        AccTopPageConfigCriteria.Criteria criteria = accTopPageConfigCriteria.createCriteria();
        criteria.andStatusEqualTo(2);
        criteria.andDeleteEqualTo(0);
        List<AccTopPageConfig> publishingList = accTopPageConfigDao.selectByCriteria(accTopPageConfigCriteria);
        if (null != publishingList && !publishingList.isEmpty()) {
            Date date = new Date();
            for (AccTopPageConfig entity : publishingList) {
                entity.setDelete(1);
                entity.setUpdatedTime(date);
                accTopPageConfigDao.updateByPrimaryKeySelective(entity);
            }
        }
        return 0;
    }

    @Override
    public AccTopPageShowDto getAccTopPage() {
        AccTopPageShowDto accTopPageShowDto = new AccTopPageShowDto();
        AccTopPageConfigCriteria accTopPageConfigCriteria = new AccTopPageConfigCriteria();
        AccTopPageConfigCriteria.Criteria criteria = accTopPageConfigCriteria.createCriteria();
        criteria.andDeleteEqualTo(0);
        List<AccTopPageConfig> accTopPageConfigList = accTopPageConfigDao.selectByCriteria(accTopPageConfigCriteria);
        if (null != accTopPageConfigList && !accTopPageConfigList.isEmpty()) {
            for (AccTopPageConfig entity : accTopPageConfigList) {
                if ("1".equals(entity.getType()) && "1".equals(entity.getStatus())) {
                    accTopPageShowDto.setFirstStatus(entity);
                } else if ("1".equals(entity.getType()) && "2".equals(entity.getStatus())) {
                    accTopPageShowDto.setFirstPreview(entity);
                } else if ("2".equals(entity.getType()) && "1".equals(entity.getStatus())) {
                    accTopPageShowDto.setSecondStatus(entity);
                } else if ("2".equals(entity.getType()) && "2".equals(entity.getStatus())) {
                    accTopPageShowDto.setSecondPreview(entity);
                }
            }
        }
        if (null != accTopPageShowDto.getFirstPreview() || null != accTopPageShowDto.getSecondPreview()) {
            accTopPageShowDto.setFlag(1);
        } else {
            accTopPageShowDto.setFlag(0);
        }

        if (null == accTopPageShowDto.getFirstPreview()) {
            accTopPageShowDto.setFirstPreview(accTopPageShowDto.getFirstStatus());
        }
        if (null == accTopPageShowDto.getSecondPreview()) {
            accTopPageShowDto.setSecondPreview(accTopPageShowDto.getSecondStatus());
        }
        return accTopPageShowDto;
    }
}
